<template>
  <div class="User-container">
    <a-form
      style="width: 600px; display: flex; align-items: center; margin: 0 auto"
      layout="inline"
      :model="formState"
      @finish="handleFinish"
      @finishFailed="handleFinishFailed"
    >
      <a-form-item style="width: 450px">
        <a-input
          size="large"
          v-model:value="formState.user"
          placeholder="输入用户信息开始查询（未输入默认查询所有）"
        ></a-input>
      </a-form-item>
      <a-form-item>
        <a-button style="height: 40px" type="primary" html-type="submit">
          查询
        </a-button>
      </a-form-item>
    </a-form>
    <ul class="list">
      <li v-for="item in list" :key="item.userId">
        <main>
          <div>
            <span>ID:{{ item.userId }}</span>
            <span>{{ item.userName }}</span>
            <span>
              {{ item.sex === 1 ? '男' : item.sex === 2 ? '女' : '保密' }}
            </span>
            <span>
              {{ item.createTime ? item.createTime.substr(0, 11) : '' }}注册
            </span>
          </div>
          <p v-if="item.memberType">
            {{
              item.memberType === 1
                ? '7'
                : item.memberType === 2
                ? '30'
                : '365'
            }}天会员
          </p>
        </main>
        <section>
          <div class="one">
            <div>
              <p>累计消费金额</p>
              <p>{{ item.payMoney || 0 }}</p>
            </div>
            <div>
              <p>累计退款金额</p>
              <p>{{ item.refundMoney || 0 }}</p>
            </div>
            <div>
              <p>累计邀请人数</p>
              <p>{{ item.invitationCount || 0 }}</p>
            </div>
          </div>
          <span></span>
          <div class="two">
            <div>
              <p>作品数量</p>
              <p>{{ item.aritcleCount }}</p>
            </div>
            <div>
              <p>作品字数</p>
              <p>{{ item.aritcleWords || 0 }}</p>
            </div>
          </div>
          <span></span>
          <div class="edit">
            <a-popover
              v-model:visible="item.visMember"
              placement="bottom"
              trigger="click"
            >
              <template #content>
                <section class="visMember-container">
                  <a-form :model="memState">
                    <a-form-item label="赠送天数：">
                      <a-select
                        v-model:value="memState.memberTypeId"
                        placeholder="选择赠送天数"
                      >
                        <a-select-option :value="1">7</a-select-option>
                        <a-select-option :value="2">30</a-select-option>
                        <a-select-option :value="3">365</a-select-option>
                        <a-select-option :value="4">90</a-select-option>
                      </a-select>
                    </a-form-item>
                    <!-- <label for="">赠送天数：</label>
                    <a-input-number
                      v-model:value="memState.num"
                      :min="1"
                      :max="365"
                    /> -->
                    <a-form-item>
                      <a-button
                        type="primary"
                        size="small"
                        @click="sure(1, item)"
                      >
                        确定
                      </a-button>
                    </a-form-item>
                  </a-form>
                </section>
              </template>
              <a-button type="primary">送会员</a-button>
            </a-popover>
            <a-popover
              v-model:visible="item.visCoupon"
              placement="bottom"
              trigger="click"
            >
              <template #content>
                <section class="visCoupon-container">
                  <a-form :model="couState">
                    <a-form-item label="优惠券  类型">
                      <a-select
                        v-model:value="couState.type"
                        placeholder="选择类型"
                      >
                        <a-select-option :value="1">查重</a-select-option>
                        <a-select-option :value="2">改写</a-select-option>
                      </a-select>
                    </a-form-item>
                    <a-form-item label="优惠券字数">2000</a-form-item>
                    <!-- <a-form-item label="邀请码">
                      <a-input
                        v-model:value="couState.code"
                        placeholder="请输入邀请码"
                      />
                    </a-form-item> -->
                    <a-form-item label="优惠券数量">
                      <a-input-number v-model:value="couState.num" :min="1" />
                    </a-form-item>

                    <a-form-item>
                      <a-button
                        type="primary"
                        size="small"
                        @click="sure(2, item)"
                      >
                        确定
                      </a-button>
                    </a-form-item>
                  </a-form>
                </section>
              </template>
              <a-button type="primary">送优惠券</a-button>
            </a-popover>
          </div>
        </section>
      </li>
    </ul>
    <a-pagination
      v-model:current="formState.pageNum"
      :total="total"
      @change="currentHandler"
      show-less-items
    />
  </div>
</template>

<script>
  import { message } from 'ant-design-vue'
  import {
    userOrderList,
    giveVip,
    sendCheckCoupon,
    sendReWriteCoupon,
  } from '../../api/table.js'
  import { reactive, ref } from 'vue'
  export default {
    name: 'User',
    setup() {
      const total = ref(0)
      const list = ref([])
      const formState = reactive({
        user: '',
        pageNum: 1,
      })
      const memState = reactive({
        num: 1,
        memberTypeId: 1,
        // date: 7,
      })
      const couState = reactive({
        type: 1,
        // code: '',
        num: 1,
      })
      const initMessage = async () => {
        list.value = []
        const { user: code, pageNum } = formState
        let res = await userOrderList({ code, pageNum })
        if (res.code) {
          console.log(res)
          total.value = res.data.total
          list.value = res.data.list
          list.value.forEach((r) => {
            r.visMember = false
            r.visCoupon = false
          })
        } else {
          message.error(res.msg)
        }
      }
      const currentHandler = (e) => {
        console.log(e)
        formState.pageNum = e
        initMessage()
      }
      const handleFinish = (values) => {
        console.log(values, formState)
        initMessage()
      }

      const handleFinishFailed = (errors) => {
        console.log(errors)
      }
      const sure = async (type, obj) => {
        console.log(type, obj)
        let res = {}
        if (type === 1) {
          obj.visMember = false
          res = await giveVip({
            memberTypeId: memState.memberTypeId,
            userId: obj.userId,
            // date: memState.date,
          })
        } else {
          if (!obj.exclusiveId) {
            message.warning('不存在邀请码')
            return
          }
          obj.visCoupon = false
          if (couState.type === 1) {
            res = await sendCheckCoupon({
              code: obj.exclusiveId,
              couponCount: couState.num,
            })
          } else {
            res = await sendReWriteCoupon({
              code: obj.exclusiveId,
              couponCount: couState.num,
            })
          }
        }
        if (res.code) {
          if (type === 1) {
            initMessage()
          } else {
            message.success(res.msg)
          }
        } else {
          message.warning(res.msg)
        }
      }
      // initMessage()
      return {
        total,
        list,
        formState,
        memState,
        couState,
        initMessage,
        currentHandler,
        handleFinish,
        handleFinishFailed,
        sure,
      }
    },
  }
</script>
<style lang="less" scoped>
  .User-container {
    > .list {
      margin: 30px auto;
      width: 86%;
      > li {
        margin: 16px auto;
        border-radius: 6px;
        border: 1px solid #ccc;
        width: 100%;
        height: 150px;
        main {
          width: 100%;
          height: 50px;
          display: flex;
          align-items: center;
          justify-content: space-between;
          font-size: 20px;
          div {
            span {
              margin: 0 12px;
            }
          }
          p {
            margin-right: 12px;
            margin-bottom: 0;
            color: rgb(243, 169, 11);
          }
        }
        section {
          width: 100%;
          height: 100px;
          display: flex;
          align-items: center;
          span {
            display: block;
            background: #ccc;
            height: 80%;
            width: 1px;
          }
          div {
            display: flex;
            align-items: center;
            justify-content: space-around;
            height: 90%;
            width: 32%;
            &.two {
              width: 28%;
            }
            > div {
              display: block;
              text-align: center;
              p {
                margin: 5px auto;
              }
            }
          }
        }
      }
    }
  }
  .visMember-container {
    width: 200px;
  }
  .visCoupon-container {
    width: 200px;
  }
</style>
